/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.pipeparser.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Vector;

/**
 *
 * @author carlos
 */
public class ComponentCoOcurrence {

    private Map getAttributesArff(String dirPath){
        Map moduleTypes = new HashMap();

        try{



            File dir = new File(dirPath);

            String[] children = dir.list();

            if(children == null){
                System.out.println("Directory does not exists");
                System.exit(1);
            }

            for(int i = 0; i < children.length; i++){
                String filename = children[i];

                String filepath = "";

                if(filename.contains("pipe")){
                    filepath = dirPath + filename;
                }else{
                    continue;
                }

                File jsonFile = new File(filepath);
                BufferedReader reader = new BufferedReader(new FileReader(jsonFile));

                Pipe pipe = new Pipe(reader);

                //Listing of modules
                LinkedList modules = pipe.getModules();
                Iterator moduleIt = modules.iterator();
                //System.out.println("==Modules==");
                while(moduleIt.hasNext()){
                    Map module = (Map)moduleIt.next();
                    String type = (String)module.get("type");

                    if(!type.contains("pipe:"))
                        moduleTypes.put(type, type);

                    //System.out.println("Module type: " + module.get("type") + "(id: " + module.get("id") + ")");
                }

            }



            Object[] atts = moduleTypes.keySet().toArray();


            System.out.println("@relation 'pipes_components'");


            for(int i = 0; i < atts.length; i++){
                String att = (String)atts[i];

                System.out.println("@attribute " + att + " {yes, no}");

                moduleTypes.put(att, i);
            }

            System.out.println("\n@data");
            

            /*
            for(Iterator it = moduleTypes.keySet().iterator(); it.hasNext(); ){
                String key = (String)it.next();
                System.out.println(key + ":" + moduleTypes.get(key));
            }
             */
            

       }catch(Exception e){

            e.printStackTrace();

       }

       return moduleTypes;
    }

    public void createHeaderArff(String dirPath){

        try{
            Map listAtts = this.getAttributesArff(dirPath);

            


            //Map moduleTypes = new HashMap();

            File dir = new File(dirPath);

            String[] children = dir.list();

            if(children == null){
                System.out.println("Directory does not exists");
                System.exit(1);
            }

            for(int i = 0; i < children.length; i++){
                String filename = children[i];

                String filepath = "";

                if(filename.contains("pipe")){
                    filepath = dirPath + filename;
                }else{
                    continue;
                }

                File jsonFile = new File(filepath);
                BufferedReader reader = new BufferedReader(new FileReader(jsonFile));

                Pipe pipe = new Pipe(reader);

                //Listing of modules
                LinkedList modules = pipe.getModules();
                Iterator moduleIt = modules.iterator();

                String[] v = new String[listAtts.size()];
                

                while(moduleIt.hasNext()){
                    Map module = (Map)moduleIt.next();
                    String type = (String)module.get("type");

                    if(!type.contains("pipe:")){
                        //moduleTypes.put(type, type);
                        //System.out.println(type + ":" + listAtts.get(type));
                        int position = (Integer)listAtts.get(type);
                        v[position] = "yes";
                    }

                }

                String strLine = "";
                for(int j = 0; j < v.length; j++){
                    if(v[j] == null){
                        strLine = strLine + "no ";
                    }else{
                        strLine = strLine + "yes ";
                    }
                }

                strLine = strLine.trim();
                System.out.println(strLine);                        
            }
        }catch(Exception e){
            
            e.printStackTrace();
            
        }


    
    }

    public void createARMinerFile(String dirPath){

        try{
            Map listAtts = this.getAttributesARMiner(dirPath);




            //Map moduleTypes = new HashMap();

            File dir = new File(dirPath);

            String[] children = dir.list();

            if(children == null){
                System.out.println("Directory does not exists");
                System.exit(1);
            }

            System.out.println("BEGIN_DATA");

            for(int i = 0; i < children.length; i++){
                String filename = children[i];

                String filepath = "";

                if(filename.contains("pipe")){
                    filepath = dirPath + filename;
                }else{
                    continue;
                }

                File jsonFile = new File(filepath);
                BufferedReader reader = new BufferedReader(new FileReader(jsonFile));

                Pipe pipe = new Pipe(reader);

                //Listing of modules
                LinkedList modules = pipe.getModules();
                Iterator moduleIt = modules.iterator();

                Map modUnique = new HashMap();

                String strLine = "";
                
                while(moduleIt.hasNext()){
                    Map module = (Map)moduleIt.next();
                    String type = (String)module.get("type");

                    modUnique.put(type, type);
                }


                for(Iterator it = modUnique.keySet().iterator(); it.hasNext(); ){
                    String type = (String)it.next();
                    if(!type.contains("pipe:")){
                        strLine = strLine + listAtts.get(type) + " ";
                    }
                }

                strLine = strLine.trim();

                System.out.println(strLine);

                /*
                String strLine = "";
                for(int j = 0; j < v.length; j++){
                    if(v[j] == null){
                        strLine = strLine + "no ";
                    }else{
                        strLine = strLine + "yes ";
                    }
                }
                 */

                strLine = strLine.trim();
                System.out.println(strLine);
            }

            System.out.println("END_DATA");

        }catch(Exception e){

            e.printStackTrace();

        }



    }

    private Map getAttributesARMiner(String dirPath){
        Map moduleTypes = new HashMap();

        try{



            File dir = new File(dirPath);

            String[] children = dir.list();

            if(children == null){
                System.out.println("Directory does not exists");
                System.exit(1);
            }

            for(int i = 0; i < children.length; i++){
                String filename = children[i];

                String filepath = "";

                if(filename.contains("pipe")){
                    filepath = dirPath + filename;
                }else{
                    continue;
                }

                File jsonFile = new File(filepath);
                BufferedReader reader = new BufferedReader(new FileReader(jsonFile));

                Pipe pipe = new Pipe(reader);

                //Listing of modules
                LinkedList modules = pipe.getModules();
                Iterator moduleIt = modules.iterator();
                //System.out.println("==Modules==");
                while(moduleIt.hasNext()){
                    Map module = (Map)moduleIt.next();
                    String type = (String)module.get("type");

                    if(!type.contains("pipe:"))
                        moduleTypes.put(type, type);

                    //System.out.println("Module type: " + module.get("type") + "(id: " + module.get("id") + ")");
                }

                reader.close();                

            }



            Object[] atts = moduleTypes.keySet().toArray();

            for(int i = 0; i < atts.length; i++){
                String att = (String)atts[i];

                int index = i + 1;
                System.out.println(index + " " + att);

                moduleTypes.put(att, index);
            }
            


       }catch(Exception e){

            e.printStackTrace();

       }

       return moduleTypes;
    }

}
